﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvvmFoundation.Wpf;
using System.Windows.Input;
using System.Windows;
using Unscrambler.Settings;

namespace Unscrambler.ViewModels
{
    public class GameViewModel : ObservableObject
    {

        private IGameboardViewModel _gameboardViewModel;
        private bool _exitCurrentGame = false;
        private bool _isGameInProgress = false;
        private bool _askIfUserWantsToRestartCurrentGame = false;
        private bool _showHelp = false;

        public IGameboardViewModel GameboardViewModel
        {
            get { return _gameboardViewModel; }
            set 
            { 
                _gameboardViewModel = value;
                //TODO: is there a better place for this?
                _gameboardViewModel.GameEnded += new EventHandler<EventArgs>(_gameboardViewModel_GameEnded);
            }
        }

        public bool IsGameInProgress
        {
            get { return _isGameInProgress; }
            set
            {
                if (value.Equals(_isGameInProgress))
                    return;

                _isGameInProgress = value;
                base.RaisePropertyChanged("IsGameInProgress");

            }
        }

        public bool AskIfUserWantsToExitCurrentGame
        {
            get { return _exitCurrentGame; }
            set
            {
                if (value.Equals(_exitCurrentGame))
                    return;

                _exitCurrentGame = value;
                base.RaisePropertyChanged("AskIfUserWantsToExitCurrentGame");

            }
        }

        public bool AskIfUserWantsToRestartCurrentGame
        {
            get { return _askIfUserWantsToRestartCurrentGame; }
            set
            {
                if (value.Equals(_askIfUserWantsToRestartCurrentGame))
                    return;

                _askIfUserWantsToRestartCurrentGame = value;
                base.RaisePropertyChanged("AskIfUserWantsToRestartCurrentGame");

            }
        }

        public bool ShowHelp
        {
            get { return _showHelp; }
            set
            {
                if (value.Equals(_showHelp))
                    return;

                _showHelp = value;
                base.RaisePropertyChanged("ShowHelp");

            }
        }

                
        public ICommand EndGameCommand
        {
            get { return new RelayCommand(ExitGame); }
        }

        public ICommand StartGameCommand
        {
            get { return new RelayCommand(StartNewGame); }
        }

        public ICommand CancelStartGameCommand
        {
            get { return new RelayCommand(CancelStartGame); }
        }

        public ICommand CancelExitGameCommand
        {
         get { return new RelayCommand(CancelExitGame);}
        }

        public ICommand SkipWordCommand
        {
            get { return new RelayCommand(SkipToNextWord, CanSkipWord); }
        }

        public ICommand ToggleHelpCommand
        {
            get
            {
                return new RelayCommand(() => ShowHelp = !ShowHelp);
            }
        }

        public ICommand GetHintCommand
        {
            get { return GameboardViewModel.GetHintCommand; }
        }

        public GameViewModel()
        {
            
        }

        void _gameboardViewModel_GameEnded(object sender, EventArgs e)
        {
            this.IsGameInProgress = false;
        }

        private void ExitGame()
        {
            if (IsGameInProgress && !AskIfUserWantsToExitCurrentGame)
            {
                this.AskIfUserWantsToExitCurrentGame = true;
            }
            else
            {
                ShutdownApp();
            }

        }

        private void CancelExitGame()
        {
            this.AskIfUserWantsToExitCurrentGame = false;
            
        }

        private void ShutdownApp()
        {
            Application.Current.Shutdown();
        }

        private void StartNewGame()
        {
            if (GameboardViewModel == null)
            {
                throw new NullReferenceException("GameboardViewModel must be set in order to start a game.");
            }
            if (IsGameInProgress && !this.AskIfUserWantsToRestartCurrentGame)
            {
                this.AskIfUserWantsToRestartCurrentGame = true;
            }
            else
            {
                this.AskIfUserWantsToRestartCurrentGame = false;
                IsGameInProgress = true;
                GameboardViewModel.StartGameCommand.Execute(null);
            }
        }

        private void CancelStartGame()
        {
            this.AskIfUserWantsToRestartCurrentGame = false;
        }

        private void SkipToNextWord()
        {
            GameboardViewModel.SkipWordCommand.Execute(null);
        }

        private bool CanSkipWord()
        {
            return _isGameInProgress && !GameboardViewModel.IsCurrentWordCorrect;
        }
    }
}
